package com.peng.stack_queue.tests;

import java.util.LinkedList;
import java.util.Queue;

/**
 * 通过queue来实现栈的功能
 */
public class QueueWrapToStack {

    private Queue<Integer> queue;
    private Queue<Integer> helpQueue;

    public QueueWrapToStack() {
        queue = new LinkedList<>();
        helpQueue = new LinkedList<>();
    }

    public static void main(String[] args) {
        QueueWrapToStack queueWrapToStack = new QueueWrapToStack();
        queueWrapToStack.push(1);
        queueWrapToStack.push(2);
        queueWrapToStack.push(3);
        queueWrapToStack.push(4);
        queueWrapToStack.push(5);

        System.out.println(queueWrapToStack.pop());
        System.out.println(queueWrapToStack.pop());
        System.out.println(queueWrapToStack.pop());
        System.out.println(queueWrapToStack.pop());
        System.out.println(queueWrapToStack.peek());
        System.out.println(queueWrapToStack.peek());
        System.out.println(queueWrapToStack.peek());
        System.out.println(queueWrapToStack.peek());


    }

    public void push(int val) {
        queue.offer(val);
    }

    public Integer pop() {
        if (queue.isEmpty()) {
            return null;
        }

        while (queue.size() > 1) {
            helpQueue.offer(queue.poll());
        }
        Integer ans = queue.poll();
        Queue<Integer> tmpQueue = queue;
        queue = helpQueue;
        helpQueue = tmpQueue;
        return ans;
    }

    public Integer peek() {
        if (queue.isEmpty()) {
            return null;
        }

        while (queue.size() > 1) {
            helpQueue.offer(queue.poll());
        }
        Integer ans = queue.poll();
        helpQueue.offer(ans);
        Queue<Integer> tmpQueue = queue;
        queue = helpQueue;
        helpQueue = tmpQueue;
        return ans;
    }


}
